package com.qianfeng.sks.findingfood.adapter;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.qianfeng.sks.findingfood.R;
import com.qianfeng.sks.findingfood.bean.ResultListListgoodsItem;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sks on 15-7-10.
 */
public class HomeAdapter extends AppBaseAdapter<ResultListListgoodsItem> {
    private BitmapUtils bitmapUtils;
    private ArrayList<ImageView> listimg;
    private HomePagerAdapter adapter;

    public HomeAdapter(Context context, List<ResultListListgoodsItem> list, BitmapUtils bitmapUtils) {
        super(context, list);
        this.bitmapUtils = bitmapUtils;
    }

    @Override
    public View getItemView(int position, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null) {
            view = inflater.inflate(R.layout.item_fragment_home, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        bitmapUtils.display(holder.imageView, list.get(position).getGoodsItem().getShop().getChefIcon());
        holder.contentView.setText(list.get(position).getGoodsItem().getDesc());
        holder.priceView.setText(Integer.parseInt(list.get(position).getGoodsItem().getPrice()) / 100 + "元");
        holder.distanceview.setText(list.get(position).getGoodsItem().getShop().getDistance());
        holder.themeView.setText(list.get(position).getGoodsItem().getName());

        adapter = new HomePagerAdapter(listimg);
        holder.viewPager.setAdapter(adapter);
        loadImg(position);
        return view;
    }

    public void loadImg(int position) {
        listimg = new ArrayList<>();
        for (int i = 0; i < list.get(position).getGoodsItem().getPicList().size(); i++) {
            ImageView imageView = new ImageView(context);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            imageView.setLayoutParams(params);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            bitmapUtils.display(imageView, list.get(position).getGoodsItem().getPicList().get(i).getThumbUrl());
            listimg.add(imageView);
        }
        adapter.notifyDataSetChanged();
    }

    private static class ViewHolder {
        @ViewInject(R.id.item_viewPager)
        ViewPager viewPager;
        @ViewInject(R.id.item_image)
        ImageView imageView;
        @ViewInject(R.id.item_content)
        TextView contentView;
        @ViewInject(R.id.item_distance)
        TextView distanceview;
        @ViewInject(R.id.item_price)
        TextView priceView;
        @ViewInject(R.id.item_theme)
        TextView themeView;

        public ViewHolder(View itemView) {
            ViewUtils.inject(this, itemView);
        }
    }


}
